<?php

    class DBIO {

        private $hostname;
        private $username;
        private $password;
        private $database;
        private $message;

        public function __construct() {
            $this->hostname = '127.0.0.1';
            $this->username = 'root';
            $this->password = 'abc123';
            $this->database = 'access';
        }

        private function connect() {
            $con = mysqli_connect($this->hostname, $this->username, $this->password, $this->database);
            $this->message = (!$con) ? mysqli_connect_error() : $con;
            return $con;
        }

        public function insert($function, $object) {
            $con = $this->connect();
            if ($con) {
                $result = $function($con, $object);
                mysqli_close($con);
                return $result;
            } else {
                return false;
            }
        }// end insert

        public function select($function, $id = false) {
            $con = $this->connect();
            if ($con) {
                $results = (!$id) ? $function($con) : $function($con, $id);
                mysqli_close($con);
                return $results;
            } else {
                return false;
            }
        }// end select

        public function update($function, $object) {
            $con = $this->connect();
            if ($con) {
                $result = $function($con, $object);
                mysqli_close($con);
                return $result;
            } else {
                return false;
            }
        }// end update

        public function delete($table, $id) {
            $sql = 'DELETE FROM ' . $table . ' WHERE id = ' . $id;
            $con = $this->connect();
            if ($con) {
                $result = mysqli_query($con, $sql);
                mysqli_close($con);
                return $result;
            } else {
                return false;
            }
        }// end delete

    }// end databaseIO
